<template>
	<view class="normal-login-container">
		<view class="logo-content">
			<image style="width: 100px;height: 100px;border-radius: 50%;" src="../../static/logo.png" mode="widthFix">
			</image>
			<br />
			<br />
			<text class="title">运油宝-司机</text>
		</view>
		<view class="login-form-content">
			<view class="input-item" style="display: flex;justify-content: center;align-items: center;">
				<view class="icon">
					<uni-icons type="flag" size="19"></uni-icons>
				</view>
				<input v-model="loginForm.dept" class="input" type="text" placeholder="服务商名称/服务商ID" maxlength="12" />
			</view>
			<view class="input-item" style="display: flex;justify-content: center;align-items: center;">
				<view class="icon">
					<uni-icons type="person" size="19"></uni-icons>
				</view>
				<input v-model="loginForm.phone" class="input" type="text" placeholder="请输入手机号" maxlength="11" />
			</view>
			<view class="input-item"
				style="display: flex;justify-content: center;align-items: center;">
				<view class="icon">
					<uni-icons type="locked" size="19"></uni-icons>
				</view>
				<input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
			</view>
			<view class="input-item" style="width: 60%;margin: 0px;display: flex;justify-content: center;align-items: center;" v-if="captchaEnabled">
				<view class="icon">
					<uni-icons type="checkbox" size="19"></uni-icons>
				</view>
				<input v-model="loginForm.code" type="number" class="input" placeholder="请输入验证码" maxlength="4" />
				<view style="height: 38px;float: right;">
					<image :src="codeUrl" @click="getCode" style="height: 38px;position: absolute;margin-left: 10px;width: 104px;"></image>
				</view>
			</view>
			<!-- <view style="display: flex;justify-content: space-between;padding-top: 10px;">
				<text @click="handleUserFindPassword">找回密码</text>
			</view> -->
			<view class="action-btn">
				<button @click="handleLogin" hover-class="btnHover" class="login-btn">登录</button>
			</view>
		</view>

	</view>
</template>

<script>
	import {getDriverId} from '@/api/system/user.js'
	import {
		getCodeImg
	} from '@/api/login'

	export default {
		data() {
			return {
				codeUrl: "",
				captchaEnabled: true,
				// 用户注册开关
				register: false,
				loginForm: {
					dept: "",
					phone: "",
					password: "",
					code:"",
					uuid: ""
				}
			}
		},
		created() {
			this.getCode()
		},
		methods: {
			// 获取图形验证码
			getCode() {
			  getCodeImg().then(res => {
			    this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled
			    if (this.captchaEnabled) {
			      this.codeUrl = 'data:image/gif;base64,' + res.data.img
			      this.loginForm.uuid = res.data.uuid
			    }
			  })
			},
			//找回密码
			handleUserFindPassword() {
				uni.navigateTo({
					url: '/pages/login/findPassword'
				})
			},
			// 登录方法
			async handleLogin() {
				if (this.loginForm.dept === "") {
					uni.showToast({
						icon: 'error',
						title: '请输入您的组织'
					})
					return
				} else if (this.loginForm.username === "") {
					uni.showToast({
						icon: 'error',
						title: '请输入您的账号'
					})
					return
				} else if (this.loginForm.password === "") {
					uni.showToast({
						icon: 'error',
						title: '请输入您的密码'
					})
					return
				} else {
					uni.showLoading({
						mask: true,
						title: '登录中...'
					})
					
					this.pwdLogin()
				}
			},
			// 密码登录
			async pwdLogin() {
				this.$store.dispatch('Login', this.loginForm).then((res) => {
					uni.hideLoading()
					this.loginSuccess()
				}).catch(error=>{
					if (this.captchaEnabled) {
					  this.getCode()
					}
				})
				
				// uni.hideLoading()
				// uni.navigateTo({
				// 	url: '/pages/login/authentication'
				// })
			},
			async getDriverId(userId){
				await getDriverId(userId).then(res=>{
					if(res.data.enableStatus=='0')//已禁用
					{
						uni.showToast({
							icon:'none',
							title:'当前司机账号被停用，无法登录'
						})
						return
					}
					uni.setStorageSync('driverInfo',res.data)
					//进入首页
					uni.switchTab({
						url:'/pages/operation/operation'
					})
				})
			},
			// 登录成功后，处理函数
			loginSuccess() {
				//设置用户信息
				this.$store.dispatch('GetInfo').then(res => {
					if(res.data.user.dept.status==='1')//当前服务商禁用状态，则跳转认证页面
					{
						
						// uni.navigateTo({
						// 	url: '/pages/login/authentication?deptId='+res.data.user.deptId
						// })
						uni.showToast({
							icon:'none',
							title:'当前服务商账号未认证，暂时无法登录'
						})
					}
					else if(res.data.user.status==='1') //如果当前账号是停用状态，则无法登录
					{
						uni.showToast({
							icon:'none',
							title:'当前用户账号被停用，无法登录'
						})
					}
					else  //跳转tabbar页面
					{
						uni.setStorageSync('AccountLogin',this.loginForm)   //存储当前账号密码，用于自动登录
						//获取当前用户id的driverId
						let storage_data=uni.getStorageSync('storage_data')
						if(storage_data)
						{
							this.getDriverId(storage_data.vuex_userInfo.userId)
						}
					}
					
				})
			}

		}
	}
</script>

<style lang="scss" scoped>
	.normal-login-container {
		width: 100%;
		height: 100vh;
		background-color: #ffffff;

		.logo-content {
			width: 100%;
			font-size: 21px;
			text-align: center;
			padding-top: 15%;

			image {
				border-radius: 4px;
			}

			.title {
				margin-left: 10px;
			}
		}

		.login-form-content {
			text-align: center;
			margin: 20px auto;
			margin-top: 15%;
			width: 80%;

			.input-item {
				margin: 20px auto;
				background-color: #f5f6f7;
				height: 45px;
				border-radius: 20px;

				.icon {
					font-size: 38rpx;
					margin-left: 10px;
					color: #999;
				}

				.input {
					width: 100%;
					font-size: 14px;
					line-height: 20px;
					text-align: left;
					padding-left: 15px;
				}

			}

			.login-btn {
				margin-top: 40px;
				height: 45px;
				width: 50%;
				border-radius: 50px;
				background-image: linear-gradient(to right, #f44817 0%, #8585ee 100%);
				color: white;
			}
		}
	}

	.btnHover {
		transform: translate(1px, 1px);
	}
</style>